/**   
 * @Title: ShuangseqiuTermInfoCommunicater.java 
 * @Package: com.esun.rabbit2.communicaters.mainthread 
 * @Description: 双色球,大乐透期号及奖池信息
 * @Author: Miko baoz@500wan.com   
 * @Date: 2011-7-5 上午11:09:26  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.communicaters.mainthread;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import com.esun.config.BussinessCode;
import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.model.DaletouInfo;
import com.esun.model.ShuangseqiuInfo;
import com.esun.network.ServerCommunicater;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.TempData;
import com.esun.util.log.EDebug;

/**
 * @ClassName: ShuangseqiuTermInfoCommunicater
 * @Description: 双色球,大乐透期号及奖池信息通讯类
 * @Author: Miko baoz@500wan.com
 * @Date: 2011-7-5 上午11:09:26
 * 
 */
public class ShuangseqiuTermInfoCommunicater extends ServerCommunicater {

	private ArrayList<Hashtable<String, String>> alCache;

	/*
	 * 服务器通讯接口
	 */
	public boolean getTermInfo(String lotid) {

		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

		list.add(mapItem(Config.PASER_MID, BussinessCode.MID_FUCAI3D_TIMELEFT));
		list.add(mapItem(Key.KEY_LOTID, lotid));

		try {
			super.getServerResponse(list, true);

			if (super.getState() == 100) {

				updateLocalCach();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	private void updateLocalCach() {
		alCache = super.getListResult();
	}

	/*
	 * 获取当前期号及奖池信息
	 */
	public Hashtable<String, String> getCurrentTermAndPool(String lotid) {

		Hashtable<String, String> htDataTmp = new Hashtable<String, String>();
		for (int i = 0; i < super.getListResult().size(); i++) {
			htDataTmp = super.getListResult().get(i);
			if (Integer.parseInt(htDataTmp.get(Key.KEY_ACTIVE)) == 1) {
				break;
			}
		}

		if (lotid.equals(Config.SHUANGSEQIU)) {
			// 存储双色球信息
			ShuangseqiuInfo.getShuangseqiuInfo().setTermNum(htDataTmp.get(TempData.EXPECT));
			EDebug.o(this, "双色球 期号 = " + htDataTmp.get(TempData.EXPECT));
			ShuangseqiuInfo.getShuangseqiuInfo().setLotteryPoolMoney(htDataTmp.get(Key.KEY_MONEY));
			if (htDataTmp.get(Key.KEY_ADDMONEY).equals(Config.NUM0) || htDataTmp.get(Key.KEY_ADDMONEY).equals(Config.NULLMARK)) {
				ShuangseqiuInfo.getShuangseqiuInfo().setAddMoney(false);
			} else {
				ShuangseqiuInfo.getShuangseqiuInfo().setAddMoney(true);
			}
		} else {

			// 存储大乐透信息
			DaletouInfo.getDaletouInfo().setTermNum(htDataTmp.get(TempData.EXPECT));
			EDebug.o(this, "大乐透 期号 = " + htDataTmp.get(TempData.EXPECT));
			DaletouInfo.getDaletouInfo().setLotteryPoolMoney(htDataTmp.get(Key.KEY_MONEY));
			if (htDataTmp.get(Key.KEY_ADDMONEY).equals(Config.NUM0) || htDataTmp.get(Key.KEY_ADDMONEY).equals(Config.NULLMARK)) {
				DaletouInfo.getDaletouInfo().setAddMoney(false);
			} else {
				DaletouInfo.getDaletouInfo().setAddMoney(true);
			}
		}

		return htDataTmp;
	}

	// 获取预售期号
	public String getNexttTerm() {

		Hashtable<String, String> htDataTmp = new Hashtable<String, String>();
		for (int i = 0; i < alCache.size(); i++) {
			htDataTmp = alCache.get(i);
			if (Integer.parseInt(htDataTmp.get(Key.KEY_ACTIVE)) == 0) {
				break;
			}
		}
		return htDataTmp.get(Key.KEY_EXPECT);
	}

	/*
	 * 参数组装
	 */
	private HashMap<String, String> mapItem(String name, String value) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(name, value);
		return map;
	}
}
